
$(function(){
	var key = getCookie('key');
	var username = getCookie('username');
	var store_id = getQueryString('store_id');
    if(!key){
        window.location.href = WapSiteUrl+'/tmpl/member/login.html';
    }
    if(!store_id){
        $.sDialog({
            skin:"red",
            content:'非法操作',
            okBtn:true,
            "okBtnText": "返回", //确定按钮的文字
            "okFn": function() { window.location.href = WapSiteUrl;}, //点击确定按钮执行的函数
        });
        return false;
    }
    $('#fixed_nav').waypoint(function() {
        $('#fixed_nav').toggleClass('fixed');
    }, {
        offset: '50'
    });

    $(document).on('click','.pay',function() {
        var money = parseFloat($('#money').val());
        var isOffline = $("#isoffline").attr("class");
        if (isNaN(money) || money <= 0) {
            $.sDialog({
                skin:"red",
                content:'支付金额必须大于0',
                okBtn:false,
                cancelBtn:false
            });
            return false;
        }
        if(isOffline){
            // 提交生成订单
            $.ajax({
                type:'POST',
                url:ApiUrl+'/index.php?act=member_buy&op=offline_pay',
                data:{key:key,order_amount:money,store_id:store_id,pay_type:1},
                dataType:'json',
                success:function(data){
                      if (data.code==200){
                            data = data.datas;
                            window.location.href = WapSiteUrl+"/tmpl/member/offline_res.html?paysn="+data.order_info['order_sn']+"&callback=1";
                      }else{
                          $.sDialog({
                              skin:"red",
                              content:'订单创建失败',
                              okBtn:false,
                              cancelBtn:false
                          });
                          return false;
                      }
                }
            })
        }else {
            toPay(money, store_id, 'member_buy', 'offline_pay');
        }
        return false;
    });


    function initPage(store_id) {
        $.ajax({
            type:"post",
            url:ApiUrl+"/index.php?act=store&op=offline_store_info",
            data:{store_id:store_id,key:key},
            dataType:"json",
            success:function(result){
                if(result.code == 200){
                    $("#store_avatar").attr('src',result.datas.store_info.store_avatar);
                    $("#store_name").html(result.datas.store_info.store_name);
                    $("#availablePredeposit").html(result.datas.member_info.available_predeposit);
                } else {
                    $.sDialog({
                        skin:"red",
                        content:result.datas.error,
                        okBtn:true,
                        "okBtnText": "返回", //确定按钮的文字
                        "okFn": function() { window.location.href = WapSiteUrl;}, //点击确定按钮执行的函数
                    });
                    return false;
                }
            }
        });
    }

    $('#money').bind('input propertychange', function() {
        var money = parseFloat($(this).val());
        $('#onlineTotal').html(money);
    });

    $('#filtrate_ul').find('a').click(function(){
        $('#filtrate_ul').find('li').removeClass('selected');
        $(this).parent().addClass('selected').siblings().removeClass("selected");
        reset = true;
        window.scrollTo(0,0);
        initPage(store_id);
    });

    //初始化页面
    initPage(store_id);
    $(window).scroll(function(){
        if(($(window).scrollTop() + $(window).height() > $(document).height()-1)){
            initPage(store_id);
        }
    });
});
function get_footer() {
    if (!footer) {
        footer = true;
        $.ajax({
            url: WapSiteUrl+'/js/tmpl/footer.js',
            dataType: "script"
          });
    }
}